package com.stylefeng.guns.modular.flagglory.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.common.persistence.model.Season;
import com.stylefeng.guns.common.persistence.dao.SeasonMapper;
import com.stylefeng.guns.core.exception.FrontException;
import com.stylefeng.guns.modular.flagglory.service.ISeasonService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * lg_赛季 服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2018-01-31
 */
@Service
@Transactional(rollbackFor = Exception.class)

public class SeasonServiceImpl extends ServiceImpl<SeasonMapper, Season> implements ISeasonService {
    @Override
    public void setDefault(Integer id, boolean isDefault) throws Exception {
        Season season = selectById(id);
        if (null == season) {
            throw new Exception("该赛季不存在！");
        }
        List<Season> seasons = season.selectList(null);
        for (Season sea : seasons) {
            if (sea.isDefault()) {
                sea.setDefault(false);
                if (!updateById(sea)) {
                    throw new Exception("修改失败！");
                }
            }
        }
        season.setDefault(isDefault);
        if (!updateById(season)) {
            throw new Exception("修改失败！");
        }
    }

    @Override
    public Season getDefaultSeason() throws FrontException{
        EntityWrapper<Season> entityWrapper = new EntityWrapper<>();
        entityWrapper.where("isdefault=true");
        Season season = selectOne(entityWrapper);
        if(null == season){
            throw new FrontException(500,"当前没有进行的赛季！");
        }
        return season;
    }
}
